package com.teacher.pc.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.entity.StaticData;
import com.teacher.pojo.entity.StuClass;
import com.teacher.pojo.vo.all.ClassLevelGetVo;
import com.teacher.service.IAllService;
import com.teacher.service.IStaticDataService;
import com.teacher.service.IStuClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zfm
 * @date 2021/9/9 10:51
 */
@RestController
@RequestMapping("/pc/excel")
@Api(tags = "excel导出")
public class ExportExcel {

    @Autowired
    private IStuClassService iStuClassService;

    @Autowired
    private IStaticDataService iStaticDataService;

    @Autowired
    private TermMapper termMapper;

    @Value("${web.excel}")
    private String excelPath;


    /**
     * 奖学金导出
     *
     * @return
     */
    @GetMapping("/reward")
    @ApiOperation("奖学金导出")
    public CommonResult<String> exportExcel() {
        List<StuClass> stuClassList = iStuClassService.getByName(null).getData();
        if (stuClassList.size() == 0) {
            return CommonResult.failed("无数据可查询");
        }
        // excel头策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteFont.setBold(false);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // excel内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 设置handler
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        /** 导出数据查询方法*/
        String fileName = UUID.randomUUID().toString() + ".xlsx";
        File file = new File(excelPath + fileName);
        ExcelWriter excelWriter = EasyExcel.write(file).build();
        Long currentTermId = termMapper.getCurrentTerm();
        if (currentTermId == null) {
            return CommonResult.failed("无默认学期");
        }
        int index = 0;
        for (StuClass stuClass : stuClassList) {
            List<StaticData> staticDataList = iStaticDataService.list(new QueryWrapper<StaticData>()
                    .lambda()
                    .eq(StaticData::getTermId, termMapper.getCurrentTerm())
                    .eq(StaticData::getClassId, stuClass.getId()));

            List<ClassLevelGetVo> classLevelGetVoList = new ArrayList<>();
            for (StaticData staticData : staticDataList) {
                ClassLevelGetVo classLevelGetVo = new ClassLevelGetVo();
                BeanUtils.copyProperties(staticData, classLevelGetVo);
                classLevelGetVoList.add(classLevelGetVo);
            }
            WriteSheet mainSheet = EasyExcel.writerSheet(index, stuClass.getClassName()).head(ClassLevelGetVo.class)
                    .registerWriteHandler(styleStrategy).build();
            excelWriter.write(classLevelGetVoList, mainSheet);
            index++;
        }

        excelWriter.finish();

        return CommonResult.success("/excel/" + fileName);
    }

}
